/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License. You may obtain a copy of the License at
	http://www.mozilla.org/MPL/
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import com.bourre.commands.Delegate;
import com.bourre.data.iterator.Iterator;

import fever.app.context.abstract.Dialog;
import fever.app.context.abstract.IDialog;
import fever.data.file.FileFilter;
import fever.data.file.FileFilterCollection;
import fever.Fever;
import fever.utils.StringUtil;

/**
 * Dialog system using <strong>SWF Studio V3</strong>.
 * 
 * <p>Usually <strong>SWF Studio V3</strong> work in synchronous mode.( result 
 * is directly return.<br />
 * But you can call alls these commands in Asynchronous mode, just adding 
 * the callback ( handler ) argument.
 *
 * @author Romain Ecarnot
 */
class fever.app.context.swfstudio.StudioDialog extends Dialog implements IDialog
{
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private var _currentPath : String;
	
		
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
		
	/**
	 * Constructor.
	 */
	public function StudioDialog()
	{
		super();
		
		_currentPath = ssGlobals.ssStartDir;
	}
	
	/**
	 * Opens a File <strong>open</strong> chooser dialog.
	 * 
	 * @param caption ( optional ) Dialog's title
	 * @param filter ( optional ) File filter to apply ( default use All acceptable files *.* )
	 * @param multipleSelect ( optional ) {@code true} to enable multiple files selections in dialog
	 * @param ( optional ) handler Callback to activate when command is Asynchronous
	 * 
	 * @return The file or file(s) selected by the user.<br />
	 * When the multipleSelect option is enabled and multiple files are returned, the list of files 
	 * will be separated by a ";" character. <br />
	 * Returns {@code null} if no selection is available.
	 */
	public function showFileOpenDialog( caption : String, filterList : FileFilterCollection, multipleSelect : Boolean, handler : Delegate ) : String
	{
		if( !multipleSelect ) multipleSelect = false;
		
		var r : Object = ssCore.App.showFileOpen(
			{ 
				caption :caption, 
				path : _currentPath,
				filter : _parseFilter( filterList ),
				multipleSelect : multipleSelect, 
				style : 'iconlist'
			}
		);
		
		var file : String;
		if ( r.success && r.result.length > 0 )
		{
			file = ( multipleSelect ) 
				? StringUtil.replace( r.result, '\r', ';' )
				: file = r.result;
			
			_currentPath = _getPathFromFile( file );
		}
		else file = null;
		
		if( handler == null ) return file;
		else
		{
			handler.addArguments( file );
			handler.execute();	
		}
	}
	
	/**
	 * Opens a File <strong>save</strong> chooser dialog.
	 * 
	 * @param caption ( optional ) Dialog's title
	 * @param filter ( optional ) File filter to apply ( default use All acceptable files *.* )
	 * @param fileName ( optional ) default filename used to populate "filename" dialog's field
	 * @param ( optional ) handler Callback to activate when command is Asynchronous
	 * 
	 * @return A fully qualified file selected by the user ( or {@code null} )
	 */
	public function showFileSaveDialog( caption : String, filterList : FileFilterCollection, fileName : String, handler : Delegate ) : String
	{	
		var args : Object = new Object();
		args.caption = caption;
		args.filter = _parseFilter( filterList );
		args.path = _currentPath;
		args.Style = 'iconlist';
		if( fileName ) args.filename = fileName;
		
		var r : Object = ssCore.App.showFileSave( args );
		var file : String;
		
		if ( r.success && r.result.length > 0 ) 
		{
			_currentPath = _getPathFromFile( r.result );
			file =  String( r.result ).substring( 0, r.result.lastIndexOf( '.' ) );
		}
		else file = null;
		
		if( handler == null ) return file;
		else
		{
			handler.addArguments( file );
			handler.execute();	
		}
	}
	
	/**
	 * Opens a Browse folder dialog.
	 * 
	 * @param caption ( optional ) Dialog's title
	 * @param ( optional ) handler Callback to activate when command is Asynchronous
	 * @param ( optional ) handler Callback to activate when command is Asynchronous
	 * 
	 * @return The path to the selected folder ( or {@code null} )
	 */
	public function showBrowseFolderDialog( caption : String, handler : Delegate ) : String
	{
		var r : Object = ssCore.App.showFolderBrowser( { title :caption, path : _currentPath } );
		
		var folder : String;
		if ( r.success && r.result.length > 0 ) 
		{
			_currentPath = _getPathFromFile( r.result );
			folder = r.result;
		}
		else folder = null;
		
		if( handler == null ) return folder;
		else
		{
			handler.addArguments( folder );
			handler.execute();	
		}
	}
	
	/**
	 * Opens a <strong>confirm</strong> dialog.
	 * 
	 * @param caption Dialog message
	 * @param ( optional ) handler Callback to activate when command is Asynchronous
	 * 
	 * @return {@code true} if user click on 'ok' button, otherwise {@code false}
	 */
	public function confirm( caption : String, handler : Delegate ) : Boolean
	{
		var r : Object = ssCore.App.showMsgBox(
			{
				icon : 'question', 
				buttons : 'YesNo', 
				defaultButton : 'button2', 
				prompt : caption, 
				title : Fever.application.getTitle() 
			} 
		);
		
		var b : Boolean = ( r.result == 'YES' ) ? true : false;
		
		if( handler == null ) return b;
		else
		{
			handler.addArguments( b );
			handler.execute();
		}
	}
	
	/**
	 * Opens a <strong>alert</strong> dialog.
	 * 
	 * @param caption Dialog message
	 * @param ( optional ) handler Callback to activate when command is Asynchronous
	 */
	public function alert( caption : String, handler : Delegate ) : Void
	{
		var r : Object = ssCore.App.showMsgBox(
			{
				icon : 'critical', 
				buttons : 'OkOnly', 
				defaultButton : 'button1', 
				prompt : caption,
				title : Fever.application.getTitle()
			}
		);
		
		if( handler ) handler.execute();
	}
	
	/**
	 * Opens a <strong>message</strong> dialog.
	 * 
	 * @param caption Dialog message
	 */
	public function message( caption : String, handler : Delegate ) : Void
	{
		var r : Object = ssCore.App.showMsgBox(
			{
				icon : 'information', 
				buttons : 'OkOnly', 
				defaultButton : 'button1', 
				prompt : caption,
				title : Fever.application.getTitle()
			}
		);
		
		if( handler ) handler.execute();
	}
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	/**
	 * SWF Studio V3 filter pattern : "Text Documents|*.doc;*.txt|Excel Doc|*.xls||"
	 */
	private function _parseFilter( filterList : FileFilterCollection ) : String
	{
		var delimiter : String = '|';
		var it : Iterator = filterList.getFilterIterator();
		var fStr : String = "";
		
		while( it.hasNext() )
		{
			var filter : FileFilter = it.next();
			
			if( !filter.isEmpty() )
				fStr += ( filter.getDescription() + delimiter + filter.getString() ) + delimiter;
		}
		
		fStr += delimiter;
		
		return fStr;
	}
	
	private function _getPathFromFile( filePath : String ) : String
	{
		return filePath.substring( 0, filePath.lastIndexOf( '\\' ) );
	}	
}